package com.syaccp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syaccp.mapper.DictMapper;
import com.syaccp.mapper.StaffMapper;
import com.syaccp.mapper.SysDeptMapper;
import com.syaccp.mapper.TOrderDetailMapper;
import com.syaccp.model.Staff;
import com.syaccp.model.SysDept;
import com.syaccp.model.TOrderDetail;

import com.syaccp.response.StaffTaskDate;
import com.syaccp.service.StaffService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private TOrderDetailMapper orderDetailMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private DictMapper dictMapper;

    @Override
    public Integer UpdateStatusInMake(Integer Id) {
        LambdaUpdateWrapper<TOrderDetail> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(TOrderDetail::getId,Id)
                .set(TOrderDetail::getStatus,1);
        int update = orderDetailMapper.update(null, lambdaUpdateWrapper);
        return update;
    }

    @Override
    public Integer UpdateStaffStatus(Staff staff) {
        LambdaUpdateWrapper<Staff> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Staff::getId,staff.getId())
                .set(Staff::getStatus,2);
        int update = staffMapper.update(null, lambdaUpdateWrapper);
        return update;
    }

    @Override
    public Staff QueryStaffById(Integer id) {
        QueryWrapper<Staff> qw=new QueryWrapper<>();
        qw.select("make_name","status","id","make_id").eq("id",id);
        Staff staff = staffMapper.selectOne(qw);
        return staff;
    }

    @Override
    public Integer AssignmentOrder(TOrderDetail orderDetail) {
        LambdaUpdateWrapper<TOrderDetail> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(TOrderDetail::getId,orderDetail.getId())
                .set(TOrderDetail::getCompanyId,orderDetail.getCompanyId())
                .set(TOrderDetail::getCompanyName,orderDetail.getCompanyName())
                .set(TOrderDetail::getMakeId,orderDetail.getMakeId())
                .set(TOrderDetail::getMakeName,orderDetail.getMakeName());
        int update = orderDetailMapper.update(null, lambdaUpdateWrapper);

        return update;
    }

    @Override
    public List<Staff> QueryStaffMsg(Staff staff) {
        QueryWrapper<Staff> qw=new QueryWrapper<>();
        qw.eq("company_id" ,staff.getCompanyId()).eq("group_id",staff.getGroupId());
        List<Staff> staff1 = staffMapper.selectList(qw);
        return staff1;
    }

    @Override
    public PageInfo<TOrderDetail> QueryWaitingTask() {
        QueryWrapper<TOrderDetail> qw=new QueryWrapper<>();
        qw.eq("status",3);
        PageHelper.offsetPage(1,5);
        return new PageInfo<>(orderDetailMapper.selectList(qw)) ;
    }

    @Override
    public List<SysDept> QueryMakeGroup(Integer id) {
        QueryWrapper<SysDept> qw=new QueryWrapper<>();
        qw.eq("parent_id",id).like("name","制作");
        List<SysDept> sysDepts = sysDeptMapper.selectList(qw);
        return sysDepts ;
    }

    @Override
    public Integer EndCurrentStaffState(Integer id) {
        LambdaUpdateWrapper<Staff> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Staff::getMakeId,id).set(Staff::getStatus,1);
        Integer rows = staffMapper.update(null,lambdaUpdateWrapper);
        return rows;
    }

    @Override
    public List<SysDept> QueryCompany() {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("parent_id",100);
        List list = sysDeptMapper.selectList(qw);
        return list;
    }

    @Override
    public StaffTaskDate QueryMakeNameTaskDate(Staff staff) {
        return staffMapper.QueryMakeNameTaskDate(staff);
    }

    @Override
    public PageInfo<Staff> QueryAllStaffByPage(Staff staff,Integer index) {
        PageHelper.startPage(index,20);
        return new PageInfo<Staff>(staffMapper.QueryAllStaffByPage(staff));
    }

    @Override
    public Integer QueryStaffMakeNum(Staff staff) {
        QueryWrapper<TOrderDetail> qw=new QueryWrapper<>();
        qw.eq("make_name",staff.getMakeName()).and(aa->aa.eq("status",1).or().eq("status",2));
        Integer integer = orderDetailMapper.selectCount(qw);
        return integer;
    }


    @Override
    public List<Staff> queryStaffInfo() {
        return staffMapper.selectList(Wrappers.lambdaQuery(Staff.class).eq(Staff::getStatus, 1));
    }
}
